package com.google.gson.internal.bind;

import com.google.gson.b.d;
import com.google.gson.j;
import com.google.gson.l;
import com.google.gson.m;
import com.google.gson.p;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public final class i extends d {
    private static final Writer a = new Writer() {
        public void close() throws IOException {
            throw new AssertionError();
        }

        public void flush() throws IOException {
            throw new AssertionError();
        }

        public void write(char[] cArr, int i, int i2) {
            throw new AssertionError();
        }
    };
    private static final p b = new p("closed");
    private final List c = new ArrayList();
    private String d;
    private j e = l.a;

    public i() {
        super(a);
    }

    private void a(j jVar) {
        if (this.d != null) {
            if (!jVar.j() || i()) {
                ((m) j()).a(this.d, jVar);
            }
            this.d = null;
        } else if (this.c.isEmpty()) {
            this.e = jVar;
        } else {
            j j = j();
            if (j instanceof com.google.gson.i) {
                ((com.google.gson.i) j).a(jVar);
                return;
            }
            throw new IllegalStateException();
        }
    }

    private j j() {
        return (j) this.c.get(this.c.size() - 1);
    }

    public d a(long j) throws IOException {
        a(new p(Long.valueOf(j)));
        return this;
    }

    public d a(Number number) throws IOException {
        if (number == null) {
            return f();
        }
        if (!g()) {
            double doubleValue = number.doubleValue();
            if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
                throw new IllegalArgumentException("JSON forbids NaN and infinities: " + number);
            }
        }
        a(new p(number));
        return this;
    }

    public d a(String str) throws IOException {
        if (this.c.isEmpty() || this.d != null) {
            throw new IllegalStateException();
        } else if (j() instanceof m) {
            this.d = str;
            return this;
        } else {
            throw new IllegalStateException();
        }
    }

    public d a(boolean z) throws IOException {
        a(new p(Boolean.valueOf(z)));
        return this;
    }

    public j a() {
        if (this.c.isEmpty()) {
            return this.e;
        }
        throw new IllegalStateException("Expected one JSON element but was " + this.c);
    }

    public d b() throws IOException {
        j iVar = new com.google.gson.i();
        a(iVar);
        this.c.add(iVar);
        return this;
    }

    public d b(String str) throws IOException {
        if (str == null) {
            return f();
        }
        a(new p(str));
        return this;
    }

    public d c() throws IOException {
        if (this.c.isEmpty() || this.d != null) {
            throw new IllegalStateException();
        } else if (j() instanceof com.google.gson.i) {
            this.c.remove(this.c.size() - 1);
            return this;
        } else {
            throw new IllegalStateException();
        }
    }

    public void close() throws IOException {
        if (this.c.isEmpty()) {
            this.c.add(b);
            return;
        }
        throw new IOException("Incomplete document");
    }

    public d d() throws IOException {
        j mVar = new m();
        a(mVar);
        this.c.add(mVar);
        return this;
    }

    public d e() throws IOException {
        if (this.c.isEmpty() || this.d != null) {
            throw new IllegalStateException();
        } else if (j() instanceof m) {
            this.c.remove(this.c.size() - 1);
            return this;
        } else {
            throw new IllegalStateException();
        }
    }

    public d f() throws IOException {
        a(l.a);
        return this;
    }
}
